/**
 *  编辑页
 *
 * 作者 admin
 * 日期 2024-08-07 16:43:01
 */
layui.use(['jquery','febs', 'form', 'validate','fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        laydate = layui.laydate,
        form = layui.form,
        table = layui.table,
        fileUpload = layui.fileUpload,
        $view = $('#reportRectifyAudit-edit'),
        $accidentType = $('div[name="accidentType"]'),
        $accidentLevel = $('div[name="accidentLevel"]'),
        $jobOperation = $('div[name="jobOperation"]'),
        $reasonType = $('div[name="reasonType"]'),
        $accidentShipType = $('div[name="accidentShipType"]'),
        injuryUserTableIns,
        validate = layui.validate;

    let tenantId = POPUP_DATA.tenantId;
    let reportId = POPUP_DATA.reportId;
    let reportReasonId = POPUP_DATA.reportReasonId;
    let injuryUserTableData = [];
    let accidentReportFileList = [];
    let accidentSurveyFileList = [];
    let accidentReasonRectifyFileList = [];
    let accidentReasonSurveyFileList = [];

    initDictItemLabel_rectifyAudit();
    form.verify(validate);
    form.render();
    initFormValue();
    initInjuryUserTable();

    laydate.render({
        elem: "#accidentDate",
        done: function (value, date) {},
        type: 'datetime'
    })

    laydate.render({
        elem: "#rectifyDeadLine",
        done: function (value, date) {},
        type: 'date'
    })

    laydate.render({
        elem: "#noticeDeptDate",
        done: function (value, date) {},
    })


    function initDictItemLabel_rectifyAudit(){
        febs.initDictItemLabel($accidentType,'SAFETY_ACCIDENT_TYPE',accidentReportData.accidentType);
        febs.initDictItemLabel($accidentLevel,'SAFETY_ACCIDENT_LEVEL',accidentReportData.accidentLevel);
        febs.initDictItemLabel($jobOperation,'SAFETY_JOB_OPERATION',accidentReportData.jobOperation);
        febs.initDictItemLabel($reasonType,'SAFETY_ACCIDENT_REASON',accidentReportReasonData.reasonType);
        febs.initDictItemLabel($accidentShipType,'SAFETY_SHIP_TYPE',accidentReportData.accidentShipType);
    }
    function initFormValue() {
        if(accidentReportReasonData){
            //事故原因调查附件
            accidentReasonSurveyFileList = accidentReportReasonData.accidentReasonSurveyFileList;
            if(accidentReasonSurveyFileList && accidentReasonSurveyFileList.length>0){
                renderFile('#uploadReportReasonSurveyBtn',accidentReasonSurveyFileList);
            }
            //事故原因整改附件
            accidentReasonRectifyFileList = accidentReportReasonData.accidentReasonRectifyFileList;
            if(accidentReasonRectifyFileList && accidentReasonRectifyFileList.length>0){
                renderFile('#uploadReportReasonRectifyBtn',accidentReasonRectifyFileList);
            }
            form.val("reportRectifyAudit-edit-form", {
                "reportReasonId": accidentReportReasonData.reportReasonId,
                "tenantId": accidentReportReasonData.tenantId,
                "reportId": accidentReportReasonData.reportId,
                "directReason": accidentReportReasonData.directReason,
                "indirectionReason": accidentReportReasonData.indirectionReason,
                "reasonContent": accidentReportReasonData.reasonContent,
                "surveyDate": accidentReportReasonData.surveyDate,
                "rectificationFlg": accidentReportReasonData.rectificationFlg,
                "rectificationStatus": accidentReportReasonData.rectificationStatus,
                "rectificationOpinion": accidentReportReasonData.rectificationOpinion,
                "rectificationDeadline": accidentReportReasonData.rectificationDeadline,
                "rectificationMeasures": accidentReportReasonData.rectificationMeasures,
                "rectificationUsers": accidentReportReasonData.rectificationUsers,
                "rectificationDate": accidentReportReasonData.rectificationDate,
                "rectificationAuditUserId": accidentReportReasonData.rectificationAuditUserId,
                "rectificationAuditRealName": accidentReportReasonData.rectificationAuditRealName,
                "rectificationAuditDate": accidentReportReasonData.rectificationAuditDate,
                "rectificationAuditRemark": accidentReportReasonData.rectificationAuditRemark,
                "cautionaryFlg": accidentReportReasonData.cautionaryFlg,
            })
            if(accidentReportReasonData.cautionaryFlg === '1'){
                $view.find('input[type=checkbox]').attr("checked","true");
            }else{
                $view.find('input[type=checkbox]').attr("checked","false");
            }
            form.render('checkbox');
        }
        if (accidentReportData) {
            //伤亡人员
            injuryUserTableData = accidentReportData.injuryUserDataList;
            //事故调查附件
            accidentSurveyFileList = accidentReportData.accidentSurveyFileList;
            if(accidentSurveyFileList && accidentSurveyFileList.length>0){
                renderFile('#uploadReportSurveyBtn',accidentSurveyFileList);
            }
            //事故上报附件
            accidentReportFileList = accidentReportData.accidentReportFileList;
            if(accidentReportFileList && accidentReportFileList.length>0){
                renderFile('#uploadReportBtn',accidentReportFileList);
            }
            if(accidentReportData.noticeDeptFlg === '1'){
                $("#noticeDeptDiv").show();
            }else{
                $("#noticeDeptDiv").hide();
            }
            form.render('checkbox');
        }
    }

    function renderFile(elem,fileList){
        fileUpload.render({
            elem:elem,
            url:ctx + 'accident/upload',
            type:'2',
            maxNum:5,
            isDetail:true,
            defaultData: fileList,
            done:function(res){
            }
            ,error: function(){
                layer.msg("服务器异常")
            }
        })
    }

    //列点击事件
    table.on('tool(accidentSurveyFileTable)', function(obj){
        var data = obj.data; // 获取当前行数据
        if(obj.event === 'download'){
            febs.download(ctx + 'accidentFile/downloadFile', data, data.fileName);
        }
    });

    form.on('radio(auditResult)',function (obj){
        if(obj.value == '1'){
            //审核通过
            $('#auditReason').addClass('febs-hide');
            $('#rectificationAuditRemark').removeClass('febs-hide');
            $view.find('textarea[name="auditReason"]').attr("lay-verify","");
            $view.find('textarea[name="rectificationAuditRemark"]').val("");
            $view.find('textarea[name="auditReason"]').val("");
        }else {
            //审核退回
            $('#auditReason').removeClass('febs-hide');
            $('#rectificationAuditRemark').addClass('febs-hide');
            $view.find('textarea[name="auditReason"]').attr("lay-verify","required")
            $view.find('textarea[name="rectificationAuditRemark"]').val("");
            $view.find('textarea[name="auditReason"]').val("");
        }

    })

    function initInjuryUserTable() {
        injuryUserTableIns = febs.table.init({
            elem: $view.find('#injuryUserTable'),
            id: 'injuryUserTable',
            page:false,
            skin: '',
            limit:Number.MAX_VALUE,
            editTrigger: 'dblclick',
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'userId', title: 'id',hide:true},
                {field: 'userName', title: 'userName',hide:true},
                {field: 'userNo', title: 'userNo',hide:true},
                {field: 'deptId', title: 'deptId',hide:true},
                {field: 'realName',align:'center', title: '姓名', minWidth: 100},
                {field: 'sex',align:'center', title: '性别',width: 60, templet: function (d) {
                        var flg = '';
                        switch (d.sex) {
                            case "0": {
                                flg = '<span>男</span>'
                                break;
                            }
                            case "1": {
                                flg = '<span>女</span>'
                                break;
                            }
                            case "2": {
                                flg = '<span>保密</span>'
                                break;
                            }
                        }
                        return flg;
                    }
                },
                {field: 'deptName',align:'center', title: '部门', minWidth: 150},
                {field: 'userType',align:'center', title: '用工形式', minWidth: 100, templet: function (d) {
                        var flg = '';
                        switch (d.userType) {
                            case "1": {
                                flg = '<span>本工</span>'
                                break;
                            }
                            case "2": {
                                flg = '<span>外包工</span>'
                                break;
                            }
                            case "3": {
                                flg = '<span>外协工</span>'
                                break;
                            }
                        }
                        return flg;
                    }
                },
                {field: 'userSeniority',align:'center', title: '工龄(年)', minWidth: 80},
                {field: 'userDegree',align:'center', title: '学历', minWidth: 80},
                {field: 'identificationNo',align:'center', title: '身份证号', minWidth: 240},
                {field: 'injuryLevel',align:'center',title: '伤害程度',minWidth: 160},
                {field: 'economyLoss',align:'center', title: '经济损失(元)', minWidth: 120}
            ]],
            done: function(res, curr, count){
            },
            data:injuryUserTableData
        });
    }

    form.on('submit(reportRectifyAudit-edit-form-submit)', function (data) {
        // accident_report_reason  rectification_status(整改状态): 1:待整改指派 2：已指派待整改 3：已退回待整改  4：已整改待审核 5：已完成
        if(data.field.auditResult =='2'){
            //退回
            data.field.rectificationStatus = '3';
        }else{
            //通过
            data.field.rectificationStatus = '5';
        }
        addOrUpdate(data.field,'提交');
        return false;
    });

    function addOrUpdate(data,operation) {
        data.tenantId = tenantId;
        data.reportReasonId = reportReasonId;
        data.reportId = reportId;
        if(data.cautionaryFlg == 'on'){
            data.cautionaryFlg = '1';
        }else{
            data.cautionaryFlg = '0';
        }
        if (data.reportId && data.reportId.length > 0) {
            febs.modal.confirm(operation+'审核结果信息','确定'+operation+'审核结果信息？', function (index) {
                febs.postArray(ctx + 'accidentReport/rectifyAudit', data, function () {
                    layer.closeAll();
                    febs.alert.success('提交成功');
                    $('#febs-accidentReportRectifyAudit').find('#query').trigger('click');
                });
            });
        }
    }
});